#######################
##This script processes the raw survey data
##It recodes variable names and values for clarity
##It applies survey weighting
#########################################

#Load Data
n<-read_dta("n.dta")

####Add binary codings for partisan id
#Republican
n$rep<-ifelse(n$partyid5 %in% c(4,5),1,0)

#Democratic
n$dem<-ifelse(n$partyid5 %in% c(1,2),1,0)

#Independent/Apolitical
n$IND<-ifelse(n$partyid5 %in% c(-1,3),1,0)
  
##re-code variable (p_over) which assigns question wording in experiments
#doing this to make it easier for interpretation
n$consequences<-ifelse(n$p_over==1,1,0)

##re-code variable (uchi2c) to tf (short for "Trump Force") to make interpretation easier
#Approval or strong approval of "The use of force is justified to restore Donald Trump to the presidency"
#p_over controls addition of [, even if some people are injured or killed].] 
n$tf<-ifelse(n$uchi2c %in% c(1,2),1,0)
n$strong_tf<-ifelse(n$uchi2c==1,1,0)

##re-code variable (uchi2e) to itf (short for "I would use Trump Force") to make interpretation easier
#Approval or strong approval of "I would participate in the use of force to restore Donald Trump to the presidency
#p_over controls addition of [, even if some people are injured or killed].] 
n$itf<-ifelse(n$uchi2e %in% c(1,2),1,0)
n$strong_itf<-ifelse(n$uchi2e==1,1,0)

##re-code variable (uchi2f) to itf (short for "I would use Trump Force") to make interpretation easier
#Approval or strong approval of "Use of force is justified to prevent the prosecution of Donald Trump for mishandling classified documents by the federal government"
#p_over controls addition of [,even if some people are injured or killed]
n$tci<-ifelse(n$uchi2f %in% c(1,2),1,0)
n$strong_tci<-ifelse(n$uchi2f==1,1,0)

#Did they pass the engagement check?
n$CORRECT<-ifelse(n$uchi8==1,1,0)
n$CORRECT<-ifelse(is.na(n$CORRECT),0,n$CORRECT)
n$INCORRECT<-ifelse(n$CORRECT==1,0,1)

#Type of Scenario
n$REP_SHOOTER<-ifelse(n$rnd_15==0,1,0)
n$DEM_SHOOTER<-ifelse(n$rnd_15==1,1,0)
n$NP_SHOOTER<-ifelse(n$rnd_15==2,1,0)

#In-Party Matching
n$d_inparty_shooter<-ifelse(n$DEM_SHOOTER*n$dem==1,1,0)
n$r_inparty_shooter<-ifelse(n$REP_SHOOTER*n$rep==1,1,0)
n$inparty_shooter<-(n$d_inparty_shooter+n$r_inparty_shooter)

#Out-Party Matching
n$r_outparty_shooter<-ifelse(n$DEM_SHOOTER*n$rep==1,1,0)
n$d_outparty_shooter<-ifelse(n$REP_SHOOTER*n$dem==1,1,0)
n$outparty_shooter<-(n$d_outparty_shooter+n$r_outparty_shooter)

#Do you support the actions of the assailant?
#SSW - support steven wright (Shooter)
n$SSW<-ifelse(n$uchi9 %in% c(1,2),1,0)

#SSSW - strongly support Steven Wright (Shooter)
n$SSSW<-ifelse(n$uchi9==1,1,0)

#WSW - Weakly Support Steven Wright (Shooter)
n$WSW<-ifelse(n$uchi9==2,1,0)

#Do you believe the assailant is justified?
n$justified<-ifelse(n$uchi10==1,1,0)



#verify weighting
nd <- svydesign(id      = ~vpsu32,
                          weights = ~weight_uchi,
                          strata = ~vstrat32,
                          nest=TRUE,
                          survey.lonely.psu = "adjust",
                          data    = n)

##Verify treatment of lonely PSUs
options(survey.adjust.domain.lonely=TRUE)
options(survey.lonely.psu="adjust")




